/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.db_Self;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.model.T_PROC_NAME;

public class Db_Common_Self
{
	public StringBuilder			sqlException	= new StringBuilder();
	public ResultSet				rs				= null;
	public PooledConnection_Self	conn_a			= null;
	public int						lastrowid		= -1;
	public int 						rowsCount		= 1;

	// / <summary>
	// / 开始事务
	// / </summary>
	public PooledConnection_Self BeginTransaction() throws SQLException
	{
		this.conn_a = DBManager_Self.getConnection();
		this.conn_a.BeginTranSaction();
		return this.conn_a;
	}

	// / <summary>
	// / 执行查询sql语句
	// / </summary>
	public ResultSet Common_Sql(StringBuilder strSql)
	{
		ResultSet rs = null;
		try
		{
			sqlException.delete(0,sqlException.length());
			this.conn_a = DBManager_Self.getConnection();
			rs = this.conn_a.executeQuery(strSql);
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
		}
		finally
		{
			this.conn_a.returnConnection();
		}
		return rs;
	}

	public ResultSet Common_Sql(PooledConnection_Self conn, StringBuilder strSql) throws SQLException
	{
		ResultSet rs = null;
		try
		{
			sqlException.delete(0,sqlException.length());
			rs = conn.executeQuery(strSql);
		}
		catch(SQLException e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			conn.RollBackTranSaction();
		}
		return rs;
	}

	public int CommonExec_SqlNoCon(PooledConnection_Self conn, StringBuilder strSql) throws SQLException
	{
		int iResult = -1;
		try
		{
			sqlException.delete(0,sqlException.length());
			iResult = conn.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			iResult = -1;
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			conn.RollBackTranSaction();
		}
		return iResult;
	}

	public int CommonExec_Sql(StringBuilder strSql)
	{
		int iResult = -1;
		try
		{
			sqlException.delete(0,sqlException.length());
			this.conn_a = DBManager_Self.getConnection();
			iResult = this.conn_a.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			iResult = -1;
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
		}
		finally
		{
			this.conn_a.returnConnection();
		}
		return iResult;
	}

	public int CommonExec_Sql(PooledConnection_Self conn, StringBuilder strSql) throws SQLException
	{
		int iResult = -1;
		try
		{
			sqlException.delete(0,sqlException.length());
			iResult = conn.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			iResult = -1;
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			conn.RollBackTranSaction();
		}
		return iResult;
	}

	public int GetMax_Main_Id(PooledConnection_Self conn, String Table_Name) throws SQLException
	{
		ResultSet rs = null;
		int iResult = 1;
		StringBuilder strSql = new StringBuilder();
		try
		{
			sqlException.delete(0,sqlException.length());
			strSql.append("select TB_KEY from T_TB_KEY where TB_NAME = '"
					+ Table_Name.toUpperCase() + "'");
			rs = Common_Sql(conn,strSql);

			if(rs.next())
			{
				iResult = rs.getInt("TB_KEY") + 1;
				strSql.delete(0,strSql.length());
				strSql.append("update T_TB_KEY set TB_KEY = '"
						+ String.valueOf(iResult) + "' where TB_NAME='"
						+ Table_Name.toUpperCase() + "'");
			}else
			{
				iResult = 1;
				strSql.delete(0,strSql.length());
				strSql.append("insert into T_TB_KEY values('"
						+ Table_Name.toUpperCase() + "','1')");
			}
			CommonExec_Sql(conn,strSql);
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			conn.RollBackTranSaction();
			iResult = 0;
		}
		finally
		{
			rs.close();
		}
		return iResult;
	}

	/*
	 * ProName 存储过程名 Parameters输入参数 ParamTypes参数类型String Int float Date
	 * ParamOutName输出参数名 ParamOutType输出参数类型
	 */
	public ResultSet Common_Sql_Proc(PooledConnection_Self conn, String ProName, String[] Parameters, String[] ParamTypes, String[] ParamOutName, String[] ParamOutType, T_PROC_NAME trn)
	{
		try
		{
			sqlException.delete(0,sqlException.length());
			if(trn.INF_TYPE.equals("1"))
				rs = conn.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
			else
			{
				rs = conn.ParamExecuteQuery(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType,new StringBuilder(trn.INF_EN_SQL));
				this.lastrowid = conn.lastrowid;
				this.rowsCount = conn_a.rowsCount;
			}
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + e.getMessage());
			Com_Fun.debug_in(e,this.toString(),"数据库操作出错请查看程序错误日志文件:" + ProName);
			try
			{
				conn.RollBackTranSaction();
			}
			catch(SQLException e1)
			{
			}
		}
		return rs;
	}

	/*
	 * ProName 存储过程名 Parameters输入参数 ParamTypes参数类型String Int float Date
	 * ParamOutName输出参数名 ParamOutType输出参数类型
	 */
	public ResultSet Common_Sql_Proc(String ProName, String[] Parameters, String[] ParamTypes, String[] ParamOutName, String[] ParamOutType, T_PROC_NAME trn)
	{
		try
		{
			sqlException.delete(0,sqlException.length());
			conn_a = DBManager_Self.getConnection();
			if(trn.INF_TYPE.equals("1"))
				rs = conn_a.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
			else
				rs = conn_a.ParamExecuteQuery(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType,new StringBuilder(trn.INF_EN_SQL));
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString(),"数据库操作出错请查看程序错误日志文件:" + ProName);
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + ProName + " "
					+ e.getMessage());
		}
		finally
		{
			conn_a.returnConnection();
		}
		return rs;
	}

	public synchronized ResultSet Common_Sql_Proc(String ProName, String[] Parameters, String[] ParamTypes, String[] ParamOutName, String[] ParamOutType) throws SQLException
	{
		try
		{
			sqlException.delete(0,sqlException.length());
			this.conn_a = DBManager_Self.getConnection();
			this.rs = this.conn_a.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + ProName + " "
					+ e.getMessage());
			Com_Fun.debug_in(e,this.toString(),"数据库操作出错请查看程序错误日志文件:" + ProName);
		}
		finally
		{
			this.conn_a.returnConnection();
		}
		return this.rs;
	}

	/**
	 * @return sqlException
	 */
	public StringBuilder getSqlException()
	{
		return sqlException;
	}

	/**
	 * @param sqlException 要设置的 sqlException
	 */
	public void setSqlException(StringBuilder sqlException)
	{
		this.sqlException = sqlException;
	}

	/**
	 * @return rs
	 */
	public ResultSet getRs()
	{
		return rs;
	}

	/**
	 * @param rs 要设置的 rs
	 */
	public void setRs(ResultSet rs)
	{
		this.rs = rs;
	}

	/**
	 * @return conn_a
	 */
	public PooledConnection_Self getConn_a()
	{
		return conn_a;
	}

	/**
	 * @param conn_a 要设置的 conn_a
	 */
	public void setConn_a(PooledConnection_Self conn_a)
	{
		this.conn_a = conn_a;
	}

	/**
	 * @return lastrowid
	 */
	public int getLastrowid()
	{
		return lastrowid;
	}

	/**
	 * @param lastrowid 要设置的 lastrowid
	 */
	public void setLastrowid(int lastrowid)
	{
		this.lastrowid = lastrowid;
	}

	/**
	 * @return rowsCount
	 */
	public int getRowsCount()
	{
		return rowsCount;
	}

	/**
	 * @param rowsCount 要设置的 rowsCount
	 */
	public void setRowsCount(int rowsCount)
	{
		this.rowsCount = rowsCount;
	}
}